﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace RemoveUnprintChar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (this.txtPath.Text == "")
            {
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.txtPath.Text = this.openFileDialog1.FileName;
                }
            }

            if (this.txtPath.Text == "")
            {
                MessageBox.Show("请输入或选择文件！", "错误");
                return;
            }
            FileInfo fileInfo = new FileInfo(this.txtPath.Text);
            if (!fileInfo.Exists)
            {
                MessageBox.Show("文件不存在！", "错误");
                return;
            }

            this.txtMessage.AppendText("开始处理文件：" + fileInfo.FullName + "...");
            StreamReader reader = new StreamReader(fileInfo.FullName);
            StringBuilder sb = new StringBuilder(reader.ReadToEnd());
            reader.Close();
            string text = "re";

            Regex regex = new Regex("'[^'.]+'");
            MatchCollection collection = regex.Matches(sb.ToString());

            for (int i = 0; i < collection.Count; i++)
            {
                sb.Replace(collection[i].Value, text + i);
            }

            StreamWriter writer = new StreamWriter(fileInfo.FullName);
            writer.Write(sb.ToString());
            writer.Close();
            this.txtMessage.AppendText("完成\r\n");
        }
    }
}
